var cph = '',openid='';
$(function(){
	openid = $('#openid').val();
	if(openid!=null&&openid!=''){
		localStorage.setItem("openid",openid);
	}else{
		openid = localStorage.getItem("openid");
	}
	if(openid==''||openid==null){
		// location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3a8350d9178db579&redirect_uri=https://h5.csnits.com/park/redirect&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
		layer.open({
			content:'请重新扫码进入'
			,skin: 'msg'
			,time: 2 //2秒后自动关闭
		});
	}
	$.ajax({
		url: '/park/data/getUserInfo?openid='+openid,
		type: 'post',
		dataType: 'json',
		success: function(res) {
			if(res.data!=null){
				cph = res.data.cph;
				var arr = cph.split('');
				for(var i in arr){
					var x = parseInt(i)+1;
					$('#p'+x).html(arr[i]);
				}
				//优惠券部分
				var couponType = getUrlParam('couponType');
				var _couponNum =  localStorage.getItem("_couponNum");
				var _couponMoney = localStorage.getItem("_couponMoney");
				if(_couponNum!=0&&_couponNum!=null&&1==couponType){
					$('#couponDiv').html('已选用'+_couponNum+'张优惠券，优惠'+_couponMoney+'元');
					showCoupon();
				}
			}
		}
	})
})

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}

//有优惠券重新渲染
function showCoupon(){
	orderNo = getUrlParam('orderNo');
	var _couponArr = localStorage.getItem("_couponArr");
	$('#orderNo').html(orderNo);
	$('#carLicense').html(cph);
	$('#duration').html(localStorage.getItem("duration"));
	$('#inTime').html(localStorage.getItem("inTime"));
	$('#outTime').html(localStorage.getItem("outTime"));
	$('#shouldPayMoney').html(localStorage.getItem("shouldPayMoney"));
	$.ajax({
		url:'/park/data/getCouponMoney',
		data:'cph='+cph+'&orderNo='+orderNo+'&data='+_couponArr,
		dataType:'json',
		type:'post',
		success:function(res){
			if('200'==res.code&&'-1'!=res.data){
				$('#shouldPayMoney1').html(res.data);
				_money = res.data;
			}else{
				layer.open({
				    content: '获取金额出错！'
				    ,skin: 'msg'
				    ,time: 2 //2秒后自动关闭
				  });
			}
		}
	})
	$('#fuck').show();
	$('#main').hide();
}
var _money = 1;
//关闭遮罩
function backUp(){
	$('#main').show();
	$('#fuck').hide();
}
var orderNo = '';
var parkTimes = 0;
var streetNo = '';
//查询在停
function select() {
	cph = $('#p1').text()+$('#p2').text()+$('#p3').text()+$('#p4').text()+$('#p5').text()+$('#p6').text()+$('#p7').text();
	var p8=$('#p8').html();
	if(p8.indexOf('绿牌')>-1){
		
	}else{
		cph+=p8;
	}
	$.ajax({
		url : '/park/data/selectOrderByCphAndStreetNo',
		data : 'cph='+cph+'&openid='+openid,
		type : 'post',
		dataType : 'json',
		success : function(res) {
			if(res.success=='true'||res.success==true){
				orderNo = res.result.orderNo;
				parkTimes = res.result.parkingTimes;
				localStorage.setItem("orderNo",res.result.orderNo);
				localStorage.setItem("duration",res.result.duration);
				localStorage.setItem("inTime",res.result.inTime);
				localStorage.setItem("outTime",res.result.outTime);
				localStorage.setItem("streetNo",res.result.streetNo);
				localStorage.setItem("shouldPayMoney",res.result.shouldPayMoney);
				localStorage.setItem("shouldPayMoney1",res.result.shouldPayMoney1);
				$('#orderNo').html(res.result.orderNo);
				$('#carLicense').html(res.result.carLicense);
				$('#duration').html(res.result.duration);
				$('#inTime').html(res.result.inTime);
				$('#outTime').html(res.result.outTime);
				streetNo = res.result.streetNo;
				if(res.result.outTime==null||res.result.outTime==''){
					$('#outTime').html(res.msg);
				}
				$('#shouldPayMoney').html(res.result.shouldPayMoney);
				_money = res.result.shouldPayMoney1*100;
				$('#shouldPayMoney').html(res.result.shouldPayMoney);
				$('#shouldPayMoney1').html(res.result.shouldPayMoney1);
				$('#fuck').show();
				$('#main').hide();
			}else{
				 layer.open({
				    content: '未查到相关订单！'
				    ,skin: 'msg'
				    ,time: 2 //2秒后自动关闭
				  });
			}
		}
	})
}

function pay(){
	var amount = _money;//;
	var payMode = 'WECHAT';
	if(parseFloat(amount)<=0.00){
		 layer.open({
		     content: '暂不需要支付'
		    ,skin: 'msg'
		    ,time: 2 //2秒后自动关闭
		});
	}else{
		var streetNo = localStorage.getItem("streetNo");
		$.ajax({
			url:'/park/data/getPayInfo',
			type:'post',
			data:'openid='+openid+'&amount='+amount+'&payMode='+payMode+'&orderNo='+orderNo+'&times='+parkTimes+'&streetNo='+streetNo+'&cph='+cph,
			dataType:'json',
			success:function(res){
				if('GLOBAL_SUCCESS'==res.data.cmdRetCode){
					d = res.data.respData;
					callpay();
				}else{
					 layer.open({
					     content: res.data.retMsg
					    ,skin: 'msg'
					    ,time: 2 //2秒后自动关闭
					});
				}
			}
		})
	}
}
var d = {};
//执行支付
function onBridgeReady(){
   WeixinJSBridge.invoke(
      'getBrandWCPayRequest', {
         "appId":d.appId,     //公众号ID，由商户传入     
         "timeStamp":d.timeStamp,         //时间戳，自1970年以来的秒数     
         "nonceStr":d.nonceStr, //随机串     
         "package":d.package,     
         "signType":d.signType,         //微信签名方式：     
         "paySign":d.paySign //微信签名 
      },
      function(res){
	      if(res.err_msg == "get_brand_wcpay_request:ok"){
	    	  layer.open({
	    		    content: '支付成功'
	    		    ,btn: '好的，么么哒',
	    		    function(){
	    		    	 location.href = '/park/logs'; 
	    		    }
	    		  }); 
	     }else{
	    	  layer.open({
    		    content: '支付没有成功！'
    		    ,btn: '好的，了解'
    		  }); 
	      } 
   }); 
}
//选择优惠券页
function _goCoupon(){
	location.href = '/park/coupon?orderNo='+orderNo;
}
//支付回调
function callpay(){
	if (typeof WeixinJSBridge == "undefined") {
	    if (document.addEventListener) {
	        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
	    } else if (document.attachEvent) {
	        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
	        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
	    }
	} else {
	    onBridgeReady();
	}
}
